package com.fap.search.meta;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.fap.search.meta.impl.DataMeta;
import com.fap.search.meta.impl.SqlFunctionMeta;
import com.fap.search.meta.impl.StringType;

@Component
public class MetaRepository {

	@Autowired
	private JdbcTemplate _jdbc;
	
	private static List<ParameterMeta> _pms=new ArrayList<ParameterMeta>();
	private static List<IFunctionMeta> _functions=new ArrayList<IFunctionMeta>();
	
	public void loadAllMeta(){
		_pms.removeAll(_pms);
		_functions.removeAll(_functions);
		
		SqlFunctionMeta sqlfunc = new SqlFunctionMeta("sql-1");
		sqlfunc.sql = "SELECT * FROM fap_bo_property_define where bo_property_id=?";
		
		ParameterMeta pm = new ParameterMeta("bo_property_id", "bo_property_id", new StringType());
		sqlfunc.addParameterMeta(pm);
		
		DataMeta dm = new DataMeta();
		dm.columns.add("bo_property_id");
		dm.columns.add("field_name");
		dm.columns.add("tag");
		sqlfunc.setDataMeta(dm);
		
		_pms.add(pm);
		_functions.add(sqlfunc);
		System.out.println("MetaRepository loadAllMeta.................");
	}
	
	public ParameterMeta getParameterMeta(String id){
		for(ParameterMeta pm : _pms){
			if(pm.getId().equals(id))
				return pm;
		}
		return null;
	}
	
	public IFunctionMeta getFunctionMeta(String id){
		System.out.println("MetaRepository getFunctionMeta: "+id);
		System.out.println("MetaRepository _functions: "+_functions.size());
		for(IFunctionMeta func : _functions){
			if(func.getId().equals(id))
				return func;
		}
		return null;
	}	
}
